package com.example.origin.demo.demo.currentprint;

import java.util.concurrent.TimeUnit;

public class AlteratePrint3 {
    static Thread threadA = null;
    static Thread threadB = null;


    public static void main(String[] args) {
        Object object = new Object();
        threadA = new Thread(() -> {
            synchronized (object) {
                for (int i = 0; i < 100; i++) {
                    if (i % 2 == 0) {
                        System.out.print(i + " ");
                        try {
                            object.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        object.notifyAll();
                    }
                }
            }

        });
        threadB = new Thread(() -> {
            synchronized (object) {
                for (int i = 0; i < 100; i++) {
                    if (i % 2 == 1) {
                        System.out.print(i + " ");
                        object.notifyAll();

                        try {
                            object.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        });

        threadA.start();
        threadB.start();


    }
}